package com.zen.payclient;

import android.os.Handler;
import android.os.Looper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * @author zeng
 * @Create at 2018/7/10
 * @Description: .
 */
public class HttpUtils {


    public static final int HTTP_RESP_SUCCESS = 1;
    public static final int HTTP_RESP_FAILED = -1;

    public static Handler sHandler;

    static {
        sHandler = new Handler(Looper.getMainLooper());
    }

    public interface IHttpRequestCallback {
        void onResult(boolean success, String result);
    }

    public static void doGet(final String strUrl, final IHttpRequestCallback pCallback) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String result;
                boolean status = false;
                try {
                    URL url = new URL(strUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(3000);
                    conn.setRequestMethod("GET");
                    conn.connect();
                    BufferedReader bufReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
                    String backData = "";
                    String line = "";
                    while ((line = bufReader.readLine()) != null)
                        backData += line + "\r\n";
                    status = true;
                    result = backData;
                } catch (IOException e) {
                    e.printStackTrace();
                    result = e.getMessage();
                }

                if (pCallback != null) {
                    pCallback.onResult(status, result);
                }

            }
        }).start();


    }

    private static String doPost(String path, Map<String, String> mData) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("Post");
            // 发送POST请求必须设置为true
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //设置连接超时时间和读取超时时间
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            out = new OutputStreamWriter(conn.getOutputStream());
            // POST的请求参数写在正文中
            if (mData != null && mData.size() > 0) {
                for (String key : mData.keySet()) {
                    out.write(key + "=" + mData.get(key) + "&");
                }
            }
            out.flush();
            out.close();
            // 取得输入流，并使用Reader读取
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result.toString();
    }

}
